window.onload = function () {
    userInformation();
    // 导航栏
    $(".menu-list .first-menu").click(function () {
        $(this).addClass("active").siblings().removeClass("active");
        $(this).find("ul").slideToggle(500);
        $(this).siblings().find("ul").hide();
    })
    $(".menu-list .second-menu").click(function () {
        var $this = $(this);
        $(".second-menu").each(function () {
            if ($(this).hasClass("current")) {
                $(this).removeClass("current");
            }
        })
        $this.addClass("current").siblings().removeClass("current");
    })
    //  阻止事件冒泡
    $(".menu-list .first-menu ul").bind("click", function (event) {
        event.stopPropagation();
    });


    /**
     * 退出登录-弹框
     * 退出登录按钮     out
     * 遮盖层     container-log
     * 确认按钮    certain
     * 取消按钮    cancel
     */
    $('.out').click(function () {
        $('.container-log').show();
        $('.certain').click(function () {
            $('.container-log').hide();
            window.location.href = "http://localhost:8080/login";
        })
        $('.cancel').click(function () {
            $('.container-log').hide();
        })
    })

    /**
     * 弹出框的隐藏与显示
     * 删除按钮   delete
     * 显示框     container
     * 取消按钮   vanish
     * 确认按钮   decision
     * 删除成功弹框   success-tip
     */
    $(document).on("click", ".delete", function (e) {
        $('.container').show();
        var deleteId = $(this).attr("value");
        var da = {
            "userId": deleteId
        }
        $('.decision').off('click').on('click',function () {
            $.ajax({
                type: 'post',
                url: '/delUser',
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(da),
                success(data) {
                    if (data == true) {
                        $('.container').hide();
                        setTimeout(function () {
                            $('.success-tip').show();
                        }, 800);
                        setTimeout(function () {
                            $('.success-tip').hide();
                        }, 1800);
                        userInformation();
                    } else {
                        $('.container').hide();
                        setTimeout(function () {
                            $('.failure-tip').show();
                        }, 800);
                        setTimeout(function () {
                            $('.failure-tip').hide();
                        }, 1800);
                    }
                    // })

                }
            })
        })
        $('.vanish').click(function () {
            $('.container').hide();
        })
    })

    /**
     * 点击编辑进入新的页面
     * 按钮名     edit
     */
    $(document).on('click', '.edit',function () {
        $('.frame').show();
        var editId = $(this).attr('value');
        var data = {
            userId: editId
        }
        $.ajax({
            type: 'get',
            url: '/selByUserId',
            dataType: 'json',
            data: data,
            success(data) {
                $('.userName').val(data.userName)
                $('.userPassowrd').val(data.password)
                $('.userNum').val(data.num)
                $('.userCompany').val(data.company)
                $('.userProfession').val(data.job)
                $('.userisManager').val(data.manager)
            }
        })

        $(".update").off('click').on('click', function () {
            var userName = $('.userName').attr('value');
            var userPassowrd = $('.userPassowrd').attr('value');
            var userNum = $('.userNum').attr('value');
            var userCompany = $('.userCompany').attr('value');
            var userProfession = $('.userProfession').attr('value');
            var userisManager = $('.userisManager').attr('value');
            var data = {
                userId: editId,
                userName: userName,
                password: userPassowrd,
                num: userNum,
                company: userCompany,
                job: userProfession,
                isManager: userisManager
            }
            // console.log(bookName)
            $.ajax({
                type: 'post',
                url: '/updateUserInfo',
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(data),
                success(data) {
                    if (data == true) {
                        $('.frame').hide();
                        setTimeout(function () {
                            $('.success-tip').show();
                        }, 800);
                        setTimeout(function () {
                            $('.success-tip').hide();
                        }, 1800);
                        userInformation()
                    } else {
                        $('.frame').hide();
                        setTimeout(function () {
                            $('.failure-tip').show();
                        }, 800);
                        setTimeout(function () {
                            $('.failure-tip').hide();
                        }, 1800);
                    }
                }
            })
        })
        $('.removal').click(function () {
            $('.frame').hide();
        })
    })

    /**
     * 点击详情功能
     * 详情    detail
     */
    // $('.detail').click(function () {
    //     window.location.href = "./user-particulars.html";
    // })

    /**
     * 点击添加跳转页面userAdd
     * 添加    btn
     */
    // $('.btn').click(function () {
    //     window.location.href = "./userAdd.html";
    // })

    /**
     * 首页展示
     */
    function userInformation() {
        $.ajax({
            type: 'get',
            url: '/selectUserInfo',
            dataType: 'json',
            success(data) {
                finding(data)
            }
        })
    }

    //展示data数据
    function finding(data) {
        var text = '';
        for (const i in data.list) {

            text += "<tr class='realitic-content' ><td>" + data.list[i].userName + "</td>"
                // "<td class='displayid'>" + "${news.id }" + "</td>"
                + "<td>" + data.list[i].password + "</td>"
                + "<td>" + data.list[i].num + "</td>"
                + "<td>" + data.list[i].company + "</td>"
                + "<td>" + data.list[i].job + "</td>"
                + "<td>" + data.list[i].manager + "</td>"
                + "<td class='functional-borrow'>" +
                "<button class='edit sames' title='编辑' type='button' value='"+ data.list[i].userId+"'> 编辑</button>" +
                "<button class='detail sames' title='详细' type='button'> 详请</button>" +
                "<button class='delete sames' title='删除' type='button' value='" + data.list[i].userId + "'> 删除</button>" +
                "</td></tr>"
        }
        $('#realContent').html(text);

        //分页数据
        if (!data.hasPreviousPage) {//处理前一页按钮
            var p = "<a id='passage' class='page-prev' value='" + data.pageNum + "'>&lt;</a>";
            // console.log("**********",data.pageNum)
        } else {
            var p = "<a id='passage' class='page-prev' value='" + data.prePage + "'>&lt;</a>";
            // console.log("-----------",data.prePage)
        }
        var g = "'page-current':''";
        var flag;
        for (var i = 1; i <= data.pages; i++) {
            flag = '';
            if (i == data.pageNum) {
                flag = "page-current";
            }
            //当前页面的按钮变成深色
            p += "<a class='" + flag + "' id='passage' value='" + i + "'>" + i + "</a>";
        }
        //处理后一页按钮
        if (data.hasNextPage) {//当当前页面由下一页的时候
            p += "<a id='passage' value='" + data.nextPage + "'>&gt;</a>"
            // console.log(data.nextPage)
        } else {
            p += "<a id='passage' value='" + data.pageNum + "'>&gt;</a>"
            // console.log(data.pageNum)
        }
        $(".page-normal").html(p);

        //总数提示
        var a = '';
        a = "<p >当前 <span text='" + data.pageNum + "'>" + data.pageNum + "</span> 页  " +
            "总 <span text='" + data.pages + "'>" + data.pages + "</span> 页  " +
            "共 <span text=‘" + data.total + "’>" + data.total + "</span> 条记录" +
            "</p>"
        $('.proceeding').html(a);

    }

    /**
     * 点击页码
     * 页码  passage
     */
    $(document).on("click", "#passage", function (e) {
        console.log($(this).attr("value"));
        var pa = $(this).attr("value");
        var data = {
            "pageNum": pa
        }
        $.ajax({
            type: 'get',
            url: '/selectUserInfo',
            dataType: 'json',
            data: data,
            success(data) {
                finding(data)
            }
        })


    })


}